﻿Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports System.Linq
Imports System.Net
Imports System.Threading.Tasks
Imports System.Web.Http
Imports System.Web.Http.Description

Namespace Controllers.ApiControllers
    Public Class ClientsController
        Inherits ApiController

        Private db As New UserInfoEntities

        ' GET: api/Clients
        Function GetClients() As IQueryable(Of Client)
            Return db.Clients
        End Function

        ' GET: api/Clients/5
        <ResponseType(GetType(Client))>
        Async Function GetClient(ByVal id As Integer) As Task(Of IHttpActionResult)
            Dim client As Client = Await db.Clients.FindAsync(id)
            If IsNothing(client) Then
                Return NotFound()
            End If

            Return Ok(client)
        End Function

        ' PUT: api/Clients/5
        <ResponseType(GetType(Void))>
        Async Function PutClient(ByVal id As Integer, ByVal client As Client) As Task(Of IHttpActionResult)
            If Not ModelState.IsValid Then
                Return BadRequest(ModelState)
            End If

            If Not id = client.ClientId Then
                Return BadRequest()
            End If

            db.Entry(client).State = EntityState.Modified

            Try
                Await db.SaveChangesAsync()
            Catch ex As DbUpdateConcurrencyException
                If Not (ClientExists(id)) Then
                    Return NotFound()
                Else
                    Throw
                End If
            End Try

            Return StatusCode(HttpStatusCode.NoContent)
        End Function

        ' POST: api/Clients
        <ResponseType(GetType(Client))>
        Async Function PostClient(ByVal client As Client) As Task(Of IHttpActionResult)
            If Not ModelState.IsValid Then
                Return BadRequest(ModelState)
            End If

            db.Clients.Add(client)
            Await db.SaveChangesAsync()

            Return CreatedAtRoute("DefaultApi", New With {.id = client.ClientId}, client)
        End Function

        ' DELETE: api/Clients/5
        <ResponseType(GetType(Client))>
        Async Function DeleteClient(ByVal id As Integer) As Task(Of IHttpActionResult)
            Dim client As Client = Await db.Clients.FindAsync(id)
            If IsNothing(client) Then
                Return NotFound()
            End If

            db.Clients.Remove(client)
            Await db.SaveChangesAsync()

            Return Ok(client)
        End Function

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing) Then
                db.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Function ClientExists(ByVal id As Integer) As Boolean
            Return db.Clients.Count(Function(e) e.ClientId = id) > 0
        End Function
    End Class
End Namespace